//
//  UserListViewController.m
//  MyCloud
//
//  Created by Visitor on 7/16/13.
//  Copyright (c) 2013 visitor.zc@gmail.com. All rights reserved.
//

#import "UserListViewController.h"
#import "Person.h"
#import "PersonInfo.h"
#import "UserListCell.h"
#import "MyBackBar.h"
@interface UserListViewController ()

@property (nonatomic, retain)NSArray *datas;

@property (nonatomic, retain)NSMutableArray *infosData;
@end

@implementation UserListViewController
{
    Person *_person;
    
}

@synthesize datas, infosData;

- (void)dealloc
{
    [super dealloc];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (id)init
{
    self = [super init];
    if(self)
    {

    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor greenColor];
    MyBackBar *myBackBar = [[MyBackBar alloc] initWithTitle:@"通讯录" andType:2];
    myBackBar.delegate = self;
    myBackBar.frame = CGRectMake(0, 44, 320, 44);
    [self.view addSubview:myBackBar];
    [myBackBar release];
    self.infosData = [NSMutableArray arrayWithCapacity:20];
    ABAddressBookRef addressBook = ABAddressBookCreate();
    PersonInfo *pi = [[PersonInfo alloc] init];
//    pi.personFirstName = @"123123";
//    pi.personLastName = @"asdasd";
    
    _person = [Person sharedPerson];
    // 得到用户列表
     self.datas = (NSArray *)[_person getPersonList];
    NSLog(@"%@ ---- ", self.datas);
    for (NSDictionary *dict in self.datas) {
        PersonInfo *info = [[PersonInfo alloc] init];
        info.personEmail  = [dict objectForKey:@"email"];
        info.personFirstName = [NSString stringWithFormat:@"%@%@", [dict objectForKey:@"lastname"],[dict objectForKey:@"firstname"]];
        info.personPhones = [dict objectForKey:@"allphone"];
        [self.infosData addObject:info];
        [info release];
    }
    
   // [_person createNewPersonWithAddressBook:addressBook andPerson:pi];

    UITableView *table = [[UITableView alloc] initWithFrame:CGRectMake(0, 88, self.view.frame.size.width, self.view.frame.size.height - 44)];
    table.delegate = self;
    table.dataSource = self;
    [self.view addSubview:table];
    [table release];
}

#pragma - mark tabelDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 85;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if ([self.datas count] > 0) {
        return [self.datas count];
    } else
        return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *cellID = @"cell";
    UserListCell *listCell = (UserListCell *)[tableView dequeueReusableCellWithIdentifier:cellID];
    if (listCell == nil) {
        listCell = [[[UserListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID] autorelease];
    }
    listCell.model  = [self.infosData objectAtIndex:indexPath.row];
    return listCell;
}

- (void)backImageClick
{
    [self.navigationController popViewControllerAnimated:YES];
}


@end
